$(function () {


    $(".err").each(function () {
        showERR($(this));
    });
    $(".input_class").focus(function () { //得到焦点清除错误信息
        var id = ($(this).attr("id"));
        ERRID = id + "ERR";
        var ele = $("#" + ERRID)
        ele.text("");
        showERR(ele);
    });
    $("#username").blur(function () { //用户名输入框失去焦点校验
        var uPattern = /^[a-zA-Z0-9_-]{4,16}$/; //4-16位字母数字下划线
        if ($(this).val() == null || $(this).val() == "") {
            //非空校验
            $("#usernameERR").text("用户名不能为空");
        } else if (!uPattern.test($(this).val())) {
            $("#usernameERR").text("用户名由4-16位字母数字下划线组成")
        }
        //查询是否重复  需要后台配合                      
        showERR($("#usernameERR"));
    })
    $("#password").blur(function () { //密码框失去焦点校验
        var uPattern = /^.*(?=.{6,})(?=.*\d)(?=.*[a-zA-Z])(?=.*[!@#$%^&*? ]).*$/; //至少6位 至少一个数字  一个字母  一个特殊符号
        var password = $(this).val();
        if (password == null || password == "") {
            $("#passwordERR").text("密码不能为空");
        } else if (!uPattern.test(password)) {
            $("#passwordERR").text("密码至少6位 由至少一个数字  一个字母  一个特殊符号组成");
        }
        showERR($("#passwordERR"));
    })
    $("#password_Guarantee").blur(function () {
        var password = $("#password").val();
        var guarantee = $("#password_Guarantee").val();
        if (guarantee == null || guarantee == "") {
            $("#password_GuaranteeERR").text("密码不能为空"); //非空检验
        }
        if (guarantee != password) {
            $("#password_GuaranteeERR").text("两次输入的密码不一致"); //一致性检测
        }
        showERR($("#password_GuaranteeERR"));

    })
    $("#submit").click(function () {
        var check = false;
        $(".err").each(function () {
            if ($(this).text() != null && $(this).text() != "") {
                console.log($(this).text())
                check = true;
            }
        })

        if (check) {
            alert("输入信息有误，请完善");
            return false;
        }

        var username = $("#username").val();
        var password = $("#password").val();
        var code = $("#verifiedCode").val();
        $.ajax({
            type: "POST",
            url: "/users/reg",
            data: {
                "username": username,
                "password": password,
                "code": code
            },
            dataType: "JSON",
            success: function (data) {
                alert(data.message);
                //刷新验证码
                var image = document.getElementById("vc");
                image.src = "/public/getverifiedcode" + "?" + Math.random();
            }
        });
    });
    $("#submit").hover(function () { // 提交按钮悬停 更换图片；
        $(this).attr("src", "../images/regist1.jpg");
    }, function () {
        $(this).attr("src", "../images/regist2.jpg");
    });
    var x = 0;
    var y = 0;
    var count = 0;
    document.onmousedown = function (event) {
        $("#words p").text("");
        var ev = event;
        console.log(event)
        x = event.clientX;
        y = event.clientY;
        var pos = count % 4;

        switch (pos) {
            case 0:
                $("#words p").css("opacity", 1);
                $("#words p").text("好").animate({opacity: 0}, 1000, "linear");
                break;
            case 1:
                $("#words p").css("opacity", 1);
                $("#words p").text("好").animate({opacity: 0}, 1000, "linear");
                break;
            case 2:
                $("#words p").css("opacity", 1);
                $("#words p").text("生").animate({opacity: 0}, 1000, "linear");
                break;
            case 3:
                $("#words p").css("opacity", 1);
                $("#words p").text("活").animate({opacity: 0}, 1000, "linear");
                break;
            default:
                break;
        }
        count++;
    }

})

function showERR(ele) {
    var text = ele.text();
    if (!text) {
        ele.hide();
    } else {
        ele.show();
    }
}
